<template>
  <div class="registerBox">
    <register-box
      @register="register"
      @getEmailCode="getEmailCode"
    ></register-box>
  </div>
</template>

<script>
import RegisterBox from './components/RegisterBox.vue'
import { sendEmailCode, userRegister, userLogin, getUserInfo } from '@/api/api'
export default {
  name: 'Register',
  components: {
    RegisterBox
  },
  methods: {
    // 点击注册按钮
    register(data) {
      userRegister(data).then((res) => {
        const userData = {
          userEmail: data.userEmail,
          password: data.password
        }
        userLogin(userData).then((res) => {
          const token = res.data.data.token
          console.log(res.data.data.token)
          this.$store.commit('setToken', token)
          getUserInfo().then((res) => {
            console.log(res)
            console.log(res.data.data)
            this.$store.commit('setUser', res.data.data)
            this.$router.push('/')
          })
        })
      })
    },
    // 点击获取邮箱验证码
    getEmailCode(userEmail, changeSendEmailBtn) {
      const data = {
        userEmail
      }
      sendEmailCode(data).then((res) => {
        changeSendEmailBtn()
      })
    }
  }
}
</script>

<style scoped>
.registerBox {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  /* background-image: url(/_nuxt/img/c2de7dc.png); */
  background-repeat: no-repeat;
  background-size: cover;
  background-image: url('https://www.yunliyunwai.cn/_nuxt/img/c2de7dc.png');
}
</style>